<template>
  <dtDialog
    title="分享"
    :visible.sync="visible"
    width="900px"
    :is-loading="dialogLoading"
    @closeDialog="visible = false" @comfirmBtn="onSubmit()"
  >
    <el-form slot="content">
      <el-form label-width="140px" label-position="left">
        <slot></slot>
        <el-form-item label="截止日期">
          <FormTemplate :form-item="{ type: 'date', format: 'yyyy-MM-dd' }" v-model="fileShareExpDate"></FormTemplate>
        </el-form-item>
        <GroupUser ref="treeDom" @getGroupUser="getGroupUser" />
      </el-form>
    </el-form>
  </dtDialog>
</template>

<script>
import GroupUser from "@/views/investment/components/GroupUser";
import FormTemplate from "@/views/investment/components/FormTemplate";
import systemApi from "@/api/investment/system";
export default {
  name: "ResourceLibraryShare",
  components: { FormTemplate, GroupUser },
  data () {
    return {
      visible: false,
      dialogLoading: false,
      info: null,
      fileShareExpDate: '',
      treeCheckedKeys: [],
      multipleSelection: [],
    }
  },
  methods: {
    open(info){
      this.visible = true
      this.info = info
      this.$nextTick(() => {
        this.$refs.treeDom.initTree()
      })
    },
    getGroupUser(info){
      this.treeCheckedKeys = info.tree
      this.multipleSelection = info.users.map(v => {
        return v.userId
      })
    },
    onSubmit(){
      if (!this.fileShareExpDate){
        this.$dtMessage({
          type: 'error',
          message: '请选择截止日期！'
        })
        return
      }
      if (!this.treeCheckedKeys.length && !this.multipleSelection.length){
        this.$dtMessage({
          type: 'error',
          message: '请选择组织或个人！'
        })
        return
      }
      let list = []
      if (!!this.treeCheckedKeys.length) list.push({
        "fileShareExpDate": this.fileShareExpDate,
        "groupOrUserId": this.treeCheckedKeys,
        "shareUserType": 2
      })
      if (!!this.multipleSelection.length) list.push({
        "fileShareExpDate": this.fileShareExpDate,
        "groupOrUserId": this.multipleSelection,
        "shareUserType": 1
      })
      const params = {
        id: this.info.id,
        list
      }
      this.dialogLoading = true
      systemApi.shareFileItem(params).then(res => {
        this.dialogLoading = false
        this.visible = false
        this.$dtMessage({
          type: 'success',
          message: '分享成功'
        })
      }).catch(e => {
        this.dialogLoading = false
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
